package app.TouTiao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import qiurank.DB;
import qiurank.Function;
import qiurank.UrlObject;

/**
 * Created by Administrator on 2015/12/31.
 */
public class UrlModel {
    /**
     * 获得未爬取url列表
     * @return 未爬取url列表
     */
    public class UrlDB{
        public String url;
        public int ispa;
        public long key;
    }
    private DB.Table urldb;
    public UrlModel(){
        urldb=DB.table("url");
    }
    public List<UrlObject> getNoUrlList(){
        return new ArrayList<UrlObject>();
    }

    /**
     * 获得已爬取url列表
     * @return
     */
    public List<UrlObject> getYesUrlList(){
        return new ArrayList<UrlObject>();
    }

    /**
     * 通过id获得url
     * @param key  id值
     * @return   url对象
     */
    public UrlDB getUrlObjectById(int key){

        try {

            ResultSet rs = urldb.where("id", key).take(100).get();
            if(rs!=null&&rs.next()){
                UrlDB ud=new UrlDB();
                ud.key=rs.getLong("id");
                ud.url=rs.getString("url");
                ud.ispa=rs.getInt("status");
                return ud;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  null;
    }
    /**
     * 判断url是否存在 ，存在返回true 否则返回false
     * @param url  url
     * @return   存在返回true 否则返回false
     */
    public boolean isExist(String _url){
        boolean isexist=false;
        if(isExistDB(_url)){
            isexist=true;
        }
        return isexist;
    }
    public boolean isExistDB(String _url){
        String md5= qiurank.Common.stringToMD5(_url);
        String md5_2="";
        if(!_url.endsWith("/")){
            md5_2= qiurank.Common.stringToMD5(_url + "/");
        }
        try {
            ResultSet rs=null;
            if(md5_2.isEmpty()) {
                rs = urldb.select("count(id) as c").where("md5", md5).take(1).get();
            }else{
                rs = urldb.select("count(id) as c").where("md5", md5).orWhere("md5",md5_2).take(1).get();
            }
            if(rs!=null&&rs.next()){
                if(rs.getInt("c")>0){
                    return true;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }


    /**
     * 往待爬取列表中增加一个url
     * @param url 要增加的url
     * @return  返回增加后的url对象
     */
    public UrlObject addUrl(final String _url, final int weight){
        final UrlObject[] urlObject = {null};
        //Log.console("正在添加"+_url+"...........");
        DB.transaction(new Function() {
            @Override
            public void transaction() throws SQLException {
                if(!isExist(_url)) {

                    Map<String,Object> map=new HashMap<String,Object>();
                    map.put("url",_url);
                    map.put("pa_time",new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
                    map.put("md5", qiurank.Common.stringToMD5(_url));
                    map.put("weight",weight);
                    long id=0;
                    //Log.console(_url+"不存在,正在插入数据库"+Common.stringToMD5(_url)+"...........");
                    try {
                        id=urldb.insertGetId(map);
                        //Log.console(_url+"插入数据库完毕id:"+id+"...........");
                        ResultSet rs=urldb.where("status",1).orderBy("weight", "desc").orderBy("id","desc").take(1).get();
                        if(rs!=null&&rs.next()){
                            urlObject[0] =new UrlObject();
                            urlObject[0].key = rs.getLong("id");
                            urlObject[0].url = rs.getString("url");
                            //Log.console("取出url:"+urlObject[0].url+".............");
                        }



                    } catch (SQLException e) {
                        e.printStackTrace();
                    }

                }
            }
        });
        if(urlObject[0] !=null){
            return urlObject[0];
        }
        //Log.console(_url+"已经存在");
        return null;
    }

    /**
     * 把url从待爬取变成已爬取
     * @param urlObject url对象
     */
    public void setPa(UrlObject urlObject){

        try {
            Map<String,Object> map=new HashMap<>();
            map.put("status", 2);
            urldb.where("id",urlObject.key).update(map);
            //xxxxxxxxxxxxxxxxxxxxxxxxx
            //urldb.truncate();
            //xxxxxxxxxxxxxxxxxxxxxxxxx
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 把url从已爬取变成待爬取
     * @param urlObject url对象
     */
    public void setNoPa(UrlObject urlObject){

    }

    public List<UrlObject> getNoUrl(){
        List<UrlObject> list=new ArrayList<>();
        try {
            ResultSet rs=urldb.where("status", 1).take(100).get();
            if(rs!=null){
                while (rs.next()){
                    UrlObject urlobj=new UrlObject();
                    urlobj.key=rs.getLong("id");
                    urlobj.url=rs.getString("url");
                    list.add(urlobj);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  list;
    }
    public int getYesUrlCount(){
        try {
            ResultSet rs=urldb.select("count(*) as c").where("status", 2).take(1).get();
            if(rs!=null&&rs.next()){
                return rs.getInt("c");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
